package net.cathackers.devel.scmp.config.impl;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

import org.apache.log4j.Logger;

import net.cathackers.devel.scmp.config.interfaces.LDAPConfig;
import net.cathackers.devel.scmp.factories.SCMPFactory;

public class XMLLDAPConfig implements LDAPConfig {
    protected Properties backend_;
    protected final String LDAP_HOST 	= "host";
    protected final String LDAP_PORT 	= "port";
    protected final String BASE_DN 	= "baseDN";
    protected final String UID 		= "uid";
    protected final String PASSWORD 	=  "password";
    protected final String BIND_DN 	= "bindDN";
    protected final String BIND_PASSWORD = "password";
    protected final String USERS_BASE = "usersBase";

    private Logger logger_;

    public XMLLDAPConfig() {
	backend_ = new Properties();
	backend_.setProperty("authType", "ldap");
	logger_ = SCMPFactory.getLogger(this);
    }

    public XMLLDAPConfig(Properties backend) {
	backend_ = backend;
	logger_ = SCMPFactory.getLogger(this);
    }

    public XMLLDAPConfig(String configFile) {
	backend_ = new Properties();
	try {
	    this.loadFromXML(configFile);
	} catch (FileNotFoundException e) {
	    logger_.error("file not found");
	    e.printStackTrace();
	} catch (IOException e) {
	    logger_.error("IO Exception ");
	    e.printStackTrace();
	}
	logger_ = SCMPFactory.getLogger(this);
    }
    
    @Override
    public void loadFromXML(String filename) throws FileNotFoundException,
	    IOException {
	try {
	    backend_.loadFromXML(new FileInputStream(filename));
	} catch (FileNotFoundException e) {
	    logger_.error(e.getMessage(), e);
	    throw e;
	} catch (IOException e) {
	    logger_.error(e.getMessage(), e);
	    throw e;
	}
    }

    @Override
    public void saveAsXML(String filename) throws FileNotFoundException,
	    IOException {
	try {
	    backend_.storeToXML(new FileOutputStream(filename),
		    "Automatically Saved by The System");

	} catch (FileNotFoundException e) {
	    logger_.error(e.getMessage(), e);
	    throw e;
	} catch (IOException e) {
	    logger_.error(e.getMessage(), e);
	    throw e;
	}

    }
    
    public Properties getBackend() {
	return backend_;
    }

    @Override
    public String getBaseDN() {
	return backend_.getProperty(BASE_DN, "dc=cat-hackers,dc=net");
    }

    @Override
    public String getBindDN() {
	return backend_.getProperty(BIND_DN, "");
    }

    @Override
    public String getBindPassword() {
	return backend_.getProperty(BIND_PASSWORD, "");
    }

    @Override
    public String getHostname() {
	return backend_.getProperty(LDAP_HOST, "localhost");
    }

    @Override
    public String getPasswordAttr() {
	return backend_.getProperty(PASSWORD, "password");
    }

    @Override
    public int getPort() {
	return Integer.parseInt(backend_.getProperty(LDAP_PORT, "389"));
    }

    @Override
    public String getUIDAttr() {
	return backend_.getProperty(UID, "uid");
    }


    @Override
    public void setBaseDN(String baseDN) {
	backend_.setProperty(BASE_DN, baseDN);

    }

    @Override
    public void setBindDN(String bindDN) {
	backend_.setProperty(BIND_DN, bindDN);
    }

    @Override
    public void setBindPassword(String dbPassword) {
	backend_.setProperty(BIND_PASSWORD, dbPassword);
    }

    @Override
    public void setHostname(String hostname) {
	backend_.setProperty(LDAP_HOST, hostname);
    }

    @Override
    public void setPasswordAttr(String passwordAttr) {
	backend_.setProperty(PASSWORD, passwordAttr);

    }

    @Override
    public void setPort(int port) {
	backend_.setProperty(LDAP_PORT, Integer.toString(port));
    }


    @Override
    public void setUIDAttr(String uidAttr) {
	backend_.setProperty(UID, uidAttr);
    }

    @Override
    public String getAuthType() {
	return backend_.getProperty(AUTH_TYPE, "ldap");
    }

    @Override
    public void setAuthType(String authType) {
	backend_.setProperty(AUTH_TYPE, authType);
    }

    @Override
    public String getUserBase() {
	return backend_.getProperty(USERS_BASE, "ou=People");
    }

    @Override
    public void setUserBase(String userBase) {
	backend_.setProperty(USERS_BASE, "ou=People");
    }

}
